project(
  'component_drivers',
  'cpp',
  version: '0.1.0',
  default_options: [
    'warning_level=3',
    'cpp_std=c++17',
    'install_inc_dir=include',
    'cpp_args=-fPIC -Wall -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-pedantic -Wno-deprecated-copy',
  ],
)

add_project_arguments('-Wall', '-Wextra', '-fvisibility=hidden', language: 'cpp')
add_project_arguments(
  '-fpermissive',
  '-Wno-error=attributes',
  '-Wno-error=deprecated-declarations',
  '-fno-strict-aliasing',
  language: 'cpp',
)

if meson.get_compiler('cpp').get_id() == 'clang'
  add_project_arguments('-Wno-#warnings', language: 'cpp')
endif

# 区分conan和meson构建
meson_build = get_option('meson_build')
# 是否为DT构建
build_tests = get_option('tests')
enable_coverage = get_option('enable_coverage')

# 代码覆盖率配置
if enable_coverage
  coverage_args = []
  coverage_link_args = []
  coverage_args = ['--coverage', '-fprofile-arcs', '-ftest-coverage', '-fprofile-update=atomic']
  coverage_link_args = ['--coverage']
  add_project_arguments(coverage_args, language: 'cpp')
  add_project_link_arguments(coverage_link_args, language: 'cpp')
  message('代码覆盖率统计已启用')
endif

if meson_build
  # 获取安装头文件目录，默认为构建目录下的include
  install_inc_dir = get_option('install_inc_dir')
  if install_inc_dir == 'include'
    # 默认值
    install_inc_dir = meson.project_build_root() / 'include'
  endif
else
  install_inc_dir = 'include'
endif

# 确保头文件安装目录存在
run_command('mkdir', '-p', install_inc_dir, check: false)

# 尝试使用 pkg-config 查找依赖
dbus_dep = dependency('dbus-1', method: 'pkg-config', required: true)
glib_dep = dependency('glib-2.0', method: 'pkg-config', required: true)
# libsoc_adapter 在DT测试中使用打桩实现，设为可选依赖
libsoc_adapter_dep = dependency('libsoc_adapter', method: 'pkg-config', required: false)

if meson_build
  libmcpp_dep = dependency(
    'libmcpp',
    fallback: ['libmcpp', 'libmcpp_dep'],
    default_options: ['tests=false'],
    required: true,
  )
else
  libmcpp_dep = dependency('libmcpp', method: 'pkg-config', required: true)
endif

include_dirs = [
  include_directories('include'),
  include_directories('gen/include'),
]

enable_conan_compile = get_option('enable_conan_compile')
add_project_arguments(
  '-DENABLE_CONAN_COMPILE=' + enable_conan_compile.to_int().to_string(),
  language: 'cpp',
)

if meson_build
  message('meson build libraries')
  lib_dir = meson.project_build_root() / 'usr/lib64'
  run_command('mkdir', '-p', lib_dir, check: false)
else
  message('bingo build libraries')
  lib_dir = 'usr/lib64'
endif

# 根据构建类型决定是否包含 libsoc_adapter 依赖
if meson_build
  # meson 构建时在DT测试中使用打桩实现，不包含 libsoc_adapter 依赖
  libmcpp_deps = [libmcpp_dep, dbus_dep, glib_dep]
else
  # 非 meson 构建时包含 libsoc_adapter 依赖（如果找到）
  if libsoc_adapter_dep.found()
    libmcpp_deps = [libmcpp_dep, dbus_dep, glib_dep, libsoc_adapter_dep]
  else
    libmcpp_deps = [libmcpp_dep, dbus_dep, glib_dep]
  endif
endif
subdir('gen')

dev_deps = [libmcpp_deps, device_tree_gen_dep]

subdir('libraries')
subdir('drivers')

if build_tests and meson_build
  message('--- start build tests ---')
  test_utilities_dep = dependency(
    'test_utilities',
    fallback: ['libmcpp', 'test_utilities_dep'],
    default_options: [],
    required: true,
  )
  subdir('tests')
endif